/**
 * Created by yangh on 2016/09/04.
 */
var main = {};
var myScroll;
var canLoad=true;
(function ($) {
	
    main.init = function () {
    	  document.addEventListener('touchmove', function (e) {
              e.preventDefault();
          }, false);
        main.initEvent();
    };

    main.initEvent = function() {
    	
    	//分类
        $("#type .conLeft li").on("click",function(e){
        	if($(this).data("id")!="-1"){
	        	//选中
	            e.stopPropagation(); 
	        	$("#type .conLeft li").removeClass("active");
	            $(this).addClass("active");
	            //隐藏左边，显示右边
	        	$("#type .conRight").hide();
	        	var target = "#type "+$(this).data("target");
        	}
        	//加载数据
        	 var categoryid=$(this).data("id");
            $("#categoryid").val(categoryid);
        	main.loadData(1);
        	
        	$(target).show();
        })
        $("#type, #desc").on("click",function(){
        	//选中
        	$(this).hide();
        })
        //商圈
        $("#desc .conLeft li").on("click",function(e){
        	
        	if($(this).data("id")!="-1"){
        		
        	
        	//选中
            e.stopPropagation(); 
        	$("#desc .conLeft li").removeClass("active");
            $(this).addClass("active");
            //隐藏左边，显示右边
        	$("#desc .conRight").hide();
        	var target = "#desc "+$(this).data("target");
        	}
        	//加载数据
        	 var districtid=$(this).data("id");
             $("#districtid").val(districtid);
        	main.loadData(1);
        	  
        	$(target).show();
        })
        
        
        //分类 右边
        $("#type .conRight li").on("click",function(){
        	//选中
        	$("type .conRight li").removeClass("active");
            $(this).addClass("active");
            
            //加载商户
             var categoryid=$(this).data("id");
             $("#categoryid").val(categoryid);
            main.loadData(1);
            
            //隐藏
            $("#type").hide();
        })
       //商圈 右边
        $("#desc .conRight li").on("click",function(){
        	//选中
        	$("desc .conRight li").removeClass("active");
            $(this).addClass("active");
            //加载商户
            var districtid=$(this).data("id");
            $("#districtid").val(districtid);
            main.loadData(1);
         	//隐藏
            $("#desc").hide();
        })
    };

    /**
     * @func move()
     * @desc 监听滚动条的位置，异步加载列表
     */
    function move() {
    	if(!canLoad){
    		return;
    	}
        if (myScroll.y < main.max + 50) {
        	canLoad=false;
        	$("#tip").text("正在加载中...");
        	//加载数据
        	main.loadData(0);
            
            this.refresh();
			main.max = this.maxScrollY;
			console.log(main.max);
        }
    }
    
    
    /** 
     * function loadData
     * 加载数据
     */
    main.loadData = function(isReplace){
    	if(isReplace==1){
    		$("#storeList").html("");
    	}
    	//ajax begin
    	 var categoryid=$("#categoryid").val();
    	 var districtid=$("#districtid").val();
    	 var index = 0;
    	 if($("#storeList li").length!=0){
    		 index=$("#storeList li:last").data("id");
    	 }
         var html = "";
         $.ajax({
     		type:"post",
     		async:true, 
        	url:path+"/vouchermarket/merchant/more",
        	data:{
        		categoryid:categoryid,
        		districtid:districtid,
        		index:index
        	},
     		success:function(data){
     			canLoad=true;
     			if(data.result.length==0){ 
     				canLoad=false;
     				$("#tip").html("商家持续入驻中"); 
     			}else if(data.result.length<=8){
     				$("#tip").html(""); 
     			}else{
     				$("#tip").html("向上滑动加载更多..."); 
     			}
     			if('000' == data.code){
     				var html = "";
     				for (var i = 0; i < data.result.length; i++) {
     					html +="<li data-id='"+data.result[i].id+"'>";
     					var url = path+"/vouchermarket/merchant/"+data.result[i].id;
 						html +="<a href='"+url+"'>";
 						html +="<div class=\"fImg\">";
 						html +="<img src='"+data.result[i].picurl+"'>";
 						html +="</div>";
 						html +="<h3>"+data.result[i].name+"</h3>";
 						html +="<p>";
 						html +="<span class=\"scoreCon\" data-score="+data.result[i].avgScore+"></span>";
 						html += "</p>";
 						html +="<p>"+data.result[i].goodsName+"</p>";
 						html +="</a>";
 						html +="</li>";
     				}	
     				
     				if(isReplace==1){
     					$("#storeList").html(html);
     				}else{
     					$("#storeList").append(html);
     				}

     				myScroll.refresh();
     	            main.max = myScroll.maxScrollY;
     				 /* 评分 */
     		        score($(".scoreCon"));
     				
     			}
     		}
     	})
   		//ajax end
    }

    /**
     * @func main.scroll()
     * @desc 配置iscroll插件
     */
    main.scroll = function () {
        myScroll = new IScroll(".storeList", {
            probeType: 3,
            mouseWheel: true,
            click: true,
            preventDefault: false
        });

        main.max = myScroll.maxScrollY;
        console.log(main.max);

        myScroll.on("scroll", move);
    };
})(jQuery);

main.init();
//加载数据
main.scroll();
main.loadData(1);
	myScroll.refresh();
     main.max = myScroll.maxScrollY;
